﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DataAccess;

namespace GUI
{
    public partial class frmTraPhong : Form
    {
        public frmTraPhong()
        {
            InitializeComponent();
        }

        HoaDonDTO hdDTO = null;
        
        private void btnTraPhong_Click(object sender, EventArgs e)
        {
            hdDTO = hoaDonDTOBindingSource.AddNew() as HoaDonDTO;
            hdDTO.MaPhieuThue = cboMaPhieuThue.SelectedValue.ToString();
            hdDTO.NgayThue = lblNgayThue.Text;
            hdDTO.NgayTra = dtpNgayTra.Text;
            hdDTO.TongNgayO = lblTongNgayO.Text;
            hdDTO.TriGia = lblTriGia.Text;
            hdDTO.NgayLap = dtpNgayLap.Text;

            DataRow dr;
            DataTable dt = new DataTable();
            dt = SqlAccess.LayBang("Phong");
            dr = dt.Rows[0];
            int iMaPhong = Convert.ToInt32(dr["MaPhong"].ToString());
            int iMaPhieu = Convert.ToInt32(cboMaPhieuThue.SelectedValue.ToString());

            string strKQ = null;
            strKQ = HoaDonBUS.Them(hdDTO);
            if (strKQ == "True")
            {
                    SqlAccess.CapNhatTrangThaiPhong(2, iMaPhong);
                    SqlAccess.CapNhatTrangThaiPhieu(2, iMaPhieu);
                    frmTraPhong_Load(sender, e);       
                    MessageBox.Show("Đã trả phòng!! Cảm ơn quý khách đã đến với khách sạn chúng tôi");
                                
                    hdDTO = null;
             }
             else
                    MessageBox.Show(strKQ, "Thông tin");
        }

        private void frmTraPhong_Load(object sender, EventArgs e)
        {
            DataSet dsPhieuThue = new DataSet();
            dsPhieuThue = SqlAccess.LayPhieu();
            cboMaPhieuThue.DataSource = dsPhieuThue.Tables[0];
            cboMaPhieuThue.ValueMember = "MaPhieuThue";
        }

        private void TinhNgayThue()
        {
            DateTime NgayTra = new DateTime();
            DateTime NgayThue = new DateTime();
            NgayThue = DateTime.Parse(lblNgayThue.Text);
            NgayTra = dtpNgayTra.Value;
            TimeSpan SoNgay = NgayTra - NgayThue;
            int ngay = Convert.ToInt32(SoNgay.TotalDays);
            lblTongNgayO.Text = ngay.ToString();
        }

        private void TinhTienPhong()
        {
            double iTien;
            double iSoNgay = Convert.ToDouble(lblTongNgayO.Text);
            double iSoTien = Convert.ToDouble(lblDonGia.Text);

            iTien = iSoNgay * iSoTien;
            lblTriGia.Text = iTien.ToString();
        }
        private void dtpNgayTra_ValueChanged(object sender, EventArgs e)
        {
            frmTraPhong_Load(sender, e);
        }

        
        private void cboMaPhieuThue_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataRow dr1;
            DataRow dr2;
            DataRow dr3;
            DataRow dr4;
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
            DataTable dt3 = new DataTable();
            DataTable dt4 = new DataTable();
            int i = cboMaPhieuThue.SelectedIndex;
            dt1 = SqlAccess.LayBang("PhieuThuePhong");
            dt2 = SqlAccess.LayBang("KhachHang");
            dt3 = SqlAccess.LayBang("Phong");
            dt4 = SqlAccess.LayBang("LoaiPhong");
            dr1 = dt1.Rows[i];
            dr2 = dt2.Rows[i];
            dr3 = dt3.Rows[i];
            dr4 = dt4.Rows[i];
            lblMaKH.Text = dr2["TenKH"].ToString();
            lblPhong.Text = dr3["TenPhong"].ToString();
            lblNgayThue.Text = dr1["NgayBatDauThue"].ToString();
            lblDonGia.Text = dr4["DonGia"].ToString();

            TinhNgayThue();
            TinhTienPhong();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult dlgHoiThoat;
            dlgHoiThoat = MessageBox.Show("Bạn có chắc thoát??", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
            if (dlgHoiThoat == DialogResult.Yes)
                this.Close();
        }

    }
}
